package com.itwxz.system.controller.pc;

import com.itwxz.common.annotation.Log;
import com.itwxz.common.config.YiLaoConfig;
import com.itwxz.common.core.controller.BaseController;
import com.itwxz.common.core.domain.AjaxResult;
import com.itwxz.common.core.domain.entity.SysUser;
import com.itwxz.common.core.domain.model.LoginUser;
import com.itwxz.common.core.page.TableDataInfo;
import com.itwxz.common.enums.BusinessType;
import com.itwxz.common.utils.SecurityUtils;
import com.itwxz.common.utils.poi.ExcelUtil;
import com.itwxz.system.domain.dto.SelectOrderDto;
import com.itwxz.system.domain.po.WxzOrder;
import com.itwxz.system.domain.po.YiLaoForm;
import com.itwxz.system.service.IWxzOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 维修站订单Controller
 *
 * @author gastronome
 * @date 2025-04-05
 */
@Tag(name = "维修站订单Controller")
@Slf4j
@RestController
@RequestMapping("/wxz/order")
public class WxzOrderController extends BaseController {
    @Autowired
    private YiLaoConfig yl;
    @Autowired
    private IWxzOrderService wxzOrderService;

    /**
     * 查询维修站订单列表
     */
    @Operation(summary = "查询维修站订单列表")
    @PreAuthorize("@ss.hasPermi('wxz:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(SelectOrderDto wxzOrder) {
        startPage();
        List<SelectOrderDto> list = wxzOrderService.selectWxzOrderList(wxzOrder);
        return getDataTable(list);
    }

    /**
     * 导出维修站订单列表
     */
    @Operation(summary = "导出维修站订单列表")
    @PreAuthorize("@ss.hasPermi('wxz:order:export')")
    @Log(title = "维修站订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SelectOrderDto wxzOrder) {
        List<SelectOrderDto> list = wxzOrderService.selectWxzOrderList(wxzOrder);
        ExcelUtil<SelectOrderDto> util = new ExcelUtil<>(SelectOrderDto.class);
        util.exportExcel(response, list, "维修站订单数据");
    }

    /**
     * 导出维修站义劳表
     */
    @Operation(summary = "导出维修站订单列表")
    @PreAuthorize("@ss.hasPermi('wxz:order:export')")
    @Log(title = "维修站订单", businessType = BusinessType.EXPORT)
    @PostMapping("/exportYiLao")
    public void exportYiLaoForm(HttpServletResponse response, SelectOrderDto wxzOrder) {
        List<YiLaoForm> list = wxzOrderService.getYiLaoList(wxzOrder);
        ExcelUtil<YiLaoForm> util = new ExcelUtil<>(YiLaoForm.class);
        util.exportExcel(response, list, "维修站订单数据", yl.getTitle());
    }

    /**
     * 获取维修站订单详细信息
     */
    @Operation(summary = "获取维修站订单详细信息")
    @PreAuthorize("@ss.hasPermi('wxz:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(wxzOrderService.getById(id));
    }

    /**
     * 新增维修站订单
     */
    @Operation(summary = "新增维修站订单")
    @PreAuthorize("@ss.hasPermi('wxz:order:add')")
    @Log(title = "维修站订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxzOrder wxzOrder) {
        return toAjax(wxzOrderService.save(wxzOrder));
    }

    /**
     * 修改维修站订单
     */
    @Operation(summary = "修改维修站订单")
    @PreAuthorize("@ss.hasPermi('wxz:order:edit')")
    @Log(title = "维修站订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxzOrder wxzOrder) {
        return toAjax(wxzOrderService.checkAndUpdateById(wxzOrder));
    }

    /**
     * 删除维修站订单
     */
    @Caching(evict = {
            @CacheEvict(value = "operate-order", key = "'list'"),
            @CacheEvict(value = "in-progress", allEntries = true),
            @CacheEvict(value = "order-statistics", allEntries = true),
            @CacheEvict(value = "order-search", allEntries = true),
            @CacheEvict(value = "order-status", allEntries = true)
    })
    @Operation(summary = "删除维修站订单")
    @PreAuthorize("@ss.hasPermi('wxz:order:remove')")
    @Log(title = "维修站订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wxzOrderService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 作废维修订单
     */
    @Operation(summary = "作废订单")
    @PreAuthorize("@ss.hasAnyPermi('wxz:order:mark-invalid')")
    @Log(title = "维修站订单", businessType = BusinessType.UPDATE)
    @PutMapping("/{ids}")
    public AjaxResult invalidateOrder(@PathVariable Long[] ids) {
        return toAjax(wxzOrderService.invalidateOrderByIds(ids));
    }

	/**
	 * 学期大清空
	 */
	@Operation(summary = "清空订单数据库")
	@PreAuthorize("@ss.hasPermi('wxz:order:remove')")
	@Log(title = "维修站订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/reset-semester")
	public AjaxResult resetSemester() {
		SysUser user = SecurityUtils.getLoginUser().getUser();
		String nickName = user.getNickName();
		log.info("{}同学 正在执行订单大清洗哦~(´⊙ω⊙`)！",  nickName);
		return toAjax(wxzOrderService.resetSemester());
	}

}
